import  React from  'react'

import  {Redirect} from 'react-router-dom'

import {deleteUserInfo}  from '../../redux/action_creators/login_action'

import  {connect} from 'react-redux'

import  {createDeleteUserInfoAction} from '../../redux/action_creators/login_action'

class Admin extends React.Component {




    //退出登录 
    logout=()=>{
     //  触发redux删除保存的用户信息 
     this.props.deleteUserInfo()
    }

    render() {

        console.log(this.props)
     const { user,isLogin} =this.props.userInfo

     if(!isLogin) {
          return    <Redirect   to='/login' /> 
     }else {
       console.log('登录了')

        
        return(
            <div>
               <div> Admin组件 我的名字是: {user.username}</div>

               <button  onClick={this.logout}>退出登录</button>
            </div>
        )

     }

    }
}

let  mapStateToProps=(state)=>{

    console.log(state)
      return  {
        userInfo:state.userInfo
      }

}

let  mapDispatchToProps=(dispatch)=>{

      return {
        deleteUserInfo:()=>dispatch(createDeleteUserInfoAction())   
      }
}




export default connect(mapStateToProps,mapDispatchToProps)(Admin)